package com.ying.core.permission;

import cn.dev33.satoken.stp.StpInterface;
import com.ying.core.exception.CustomException;
import com.ying.core.response.ResultCode;
import com.ying.po.system.User;
import com.ying.service.system.PermissionService;
import com.ying.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private UserService userService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public List<String> getPermissionList(Object o, String s) {
        Integer id = Integer.valueOf((String) o);
        User user = userService.findById(id);
        if (user == null) {
            throw new CustomException(ResultCode.USER_NOT_EXIST, "StpInterfaceImpl.getPermissionList");
        }
        List<String> permissions = permissionService.findPermissionsByUserType(user.getType());
        return permissions;
    }

    @Override
    public List<String> getRoleList(Object o, String s) {
        Integer id = Integer.valueOf((String) o);
        User user = userService.findById(id);
        List<String> roles = new ArrayList<>();
        if (user.getType() == 1) {
            roles.add("admin");
        } else if (user.getType() == 0) {
            roles.add("user");
        }
        return roles;
    }
}
